/*
 * input.cpp
 *
 *  Created on: 24-Apr-2009
 *      Author: catchthealien
 */

#include <cssysdef.h>
#include <crystalspace.h>

#include <iostream>
#include <stdio.h>

#include "cs.h"
#include "input.h"

namespace cta {

input::input() {
	// Initialize Keyboard Driver
	kbd_ = csQueryRegistry<iKeyboardDriver> (cs::c().obj_reg);
	  if (!kbd_) std::cout << "Failed to locate Keyboard Driver!" << std::endl;
	joy_ = csQueryRegistry<iJoystickDriver> (cs::c().obj_reg);
	  if (!joy_) std::cout << "Failed to locate JoyStick Driver!" << std::endl;
	joy_->Reset();

	for (unsigned int i=0; i<255; ++i)
	                        KeyIsDown[i] = false;
}

bool input::OnKeyboard(iEvent& ev)
{

	// We got a keyboard event.
	csKeyEventType eventtype = csKeyEventHelper::GetEventType(&ev);
	if (eventtype == csKeyEventTypeDown)
	{
    // The user pressed a key (as opposed to releasing it).
		utf32_char code = csKeyEventHelper::GetCookedCode(&ev);
		KeyIsDown[code] = csKeyEventTypeDown;
		//std::cout << "KeyDown" << std::endl;
	}
	return false;
}
// This is used to check whether a key is being held down
bool input::IsKeyDown(utf32_char keyCode) const
{
        return KeyIsDown[keyCode];
}

bool input::IsKeyPressed(utf32_char keyCode)
{
	return(kbd_->GetKeyState(keyCode));
}

bool input::Init()
{
	//cta::graphics::device->setEventReceiver(this);
	return true;
}

bool input::JoyLeft()
{
	if(joy_)
		if(((joy_->GetLast(0,0))<-1)&&((joy_->GetLast(1,0))>=-32767)) return true;
	return false;
}
bool input::JoyRight()
{
	if(joy_)
		if(((joy_->GetLast(0,0))>1)&&((joy_->GetLast(1,0))<=32767)) return true;
	return false;
}

bool input::JoyUp()
{
	if(joy_)
		if(((joy_->GetLast(0,1))<-1)&&((joy_->GetLast(1,1))>=-32767)) return true;
	return false;
}
bool input::JoyDown()
{
	if(joy_)
		if(((joy_->GetLast(0,1))>1)&&((joy_->GetLast(1,1))<=32767)) return true;
	return false;
}

input::~input() {
	// TODO Auto-generated destructor stub
}

}
